<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model.trim="search_form.username" placeholder="操作人" style="width: 150px;" clearable />
    <el-input v-model.trim="search_form.operation" placeholder="操作内容" style="width: 200px;" clearable />
      <el-date-picker
        v-model="date_range"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        style="width: 300px;"
      />
      <el-button type="primary" icon="el-icon-search" @click="handle_search">搜索</el-button>
      <el-button @click="handle_reset">重置</el-button>
      <el-button type="danger" icon="el-icon-delete" @click="handle_clean">清空日志</el-button>
    </div>

    <el-table
      v-loading="loading"
      :data="log_list"
      style="width: 100%"
      border
    >
      <el-table-column prop="id" label="ID" />
    <el-table-column prop="username" label="操作人" />
    <el-table-column prop="ip" label="IP地址" />
      <el-table-column prop="operation" label="操作内容" />
      <el-table-column prop="method" label="请求方法" />
    <el-table-column prop="params" label="请求参数">
        <template slot-scope="scope">
          <el-tooltip :content="scope.row.params" placement="top">
            <span>{{ scope.row.params.length > 20 ? scope.row.params.substring(0, 20) + '...' : scope.row.params }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="time" label="耗时(ms)" />
    <el-table-column prop="created" label="操作时间" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handle_detail(scope.row.id)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <div class="pagination-container">
      <el-pagination
        background
        layout="prev, pager, next, jumper"
        :total="total"
        :page-size="page_size"
        :current-page="page_num"
        @current-change="handle_current_change"
        @size-change="handle_size_change"
      />
    </div>

    <!-- 详情对话框 -->
    <el-dialog
      title="操作日志详情"
      :visible.sync="detail_visible"
      width="600px"
    >
      <el-descriptions :column="1" border>
        <el-descriptions-item label="日志ID">{{ detail_data.id }}</el-descriptions-item>
        <el-descriptions-item label="操作人">{{ detail_data.username }}</el-descriptions-item>
        <el-descriptions-item label="IP地址">{{ detail_data.ip }}</el-descriptions-item>
        <el-descriptions-item label="操作内容">{{ detail_data.operation }}</el-descriptions-item>
        <el-descriptions-item label="请求方法">{{ detail_data.method }}</el-descriptions-item>
        <el-descriptions-item label="请求参数">{{ detail_data.params }}</el-descriptions-item>
        <el-descriptions-item label="耗时(ms)">{{ detail_data.time }}</el-descriptions-item>
        <el-descriptions-item label="操作时间">{{ detail_data.created }}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detail_visible = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import sysOperationLogApi from '@/api/sys-operation-log'

export default {
  name: 'OperationLogManagement',
  data() {
    return {
      loading: false,
      log_list: [],
      total: 0,
      page_num: 1,
      page_size: 10,
      date_range: [],
      search_form: {
        username: '',
        operation: ''
      },
      detail_visible: false,
      detail_data: {}
    }
  },
  created() {
    this.get_list()
  },
  methods: {
    // 获取操作日志列表
    async get_list() {
      this.loading = true
      try {
        const params = {
          page_num: this.page_num,
          page_size: this.page_size,
          ...this.search_form
        }
        
        // 添加日期范围
        if (this.date_range && this.date_range.length === 2) {
          params.start_time = this.date_range[0]
          params.end_time = this.date_range[1]
        }
        
        const { data } = await sysOperationLogApi.list(params)
        this.log_list = data.records
        this.total = data.total
      } catch (error) {
        this.$message.error('获取操作日志列表失败')
      } finally {
        this.loading = false
      }
    },
    
    // 搜索
    handle_search() {
      this.page_num = 1
      this.get_list()
    },
    // 重置搜索条件
    handle_reset() {
      this.search_form = {
        username: '',
        operation: ''
      }
      this.date_range = []
      this.get_list()
    },
    
    // 查看详情
    async handle_detail(id) {
      try {
        const { data } = await sysOperationLogApi.detail(id)
        this.detail_data = data
        this.detail_visible = true
      } catch (error) {
        this.$message.error('获取详情失败')
      }
    },
    
    // 清空日志
    async handle_clean() {
      this.$confirm('确定要清空所有操作日志吗？此操作不可恢复！', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          await sysOperationLogApi.clean()
          this.$message.success('清空成功')
          this.get_list()
        } catch (error) {
          this.$message.error('清空失败')
        }
      })
    },
    
    // 分页处理
    handle_current_change(val) {
      this.page_num = val
      this.get_list()
    },
    
    handle_size_change(val) {
      this.page_size = val
      this.page_num = 1
      this.get_list()
    }
  }
}
</script>

<style scoped>
.filter-container {
  padding: 10px 0;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>